<?php

namespace Apps\AppApi\Method\MyBranch;

use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\Keyword;
use Apps\Common\Libs\UploadPic;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\CosQKeyword;
use Apps\Common\Models\BeautyParlorService;
use Phalcon\Mvc\Model;


/**
 * 修改|发布服务
 * @author      545
 * @date        2016年3月12日
 * @version     1.1.2
 * @copyright   Copyright 2015 meelier.com
 */
class Modify extends BaseMethod
{
    
    private $success = 0;
    private $id = 0;

    public function run()
    {
    	/**
         * 验证用户权限
         */
        if(false == $this->verifyUserAuth()){
            return false;
        }
        $bp_id = intval($this -> getDataItem('bp_id',0));
        
        //服务名称
        $service_name = $this->getDataItem('service_name');
        $service_price = $this->getDataItem('service_price');
        $service_unit = $this->getDataItem('service_unit');
        
        $comment_info = Keyword::Filter($this->getDataItem('content',''));
        $comment_info = trim($comment_info);
        //关键字替换
        $keyword = CosQKeyword::query() -> execute() -> toArray();
        foreach ($keyword as $k => $v){
        
            $comment_info = str_replace($v['keyword'], '***', $comment_info);
        
        }
        $service_id = $this->getDataItem('service_id',0);

        $up = new UploadPic();
        $bp_cover = $up->beautyParlorCover();
        
        $bp_cover = $bp_cover[0];
        $data = [];
        //如果是新增服务 则检查参数
        if ($service_id==0){
            if (empty($service_name)||empty($bp_cover)||empty($comment_info)||empty($bp_id)){
                return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR,"数据不全");
            }
        }else {//修改服务则验证服务是否存在
            $data = BeautyParlorService::findFirst($service_id);
        }

        if($data) {//服务存在则修改
            
            if (!empty($service_name)){
                $temp['service_name'] = $service_name;
            }
            if (!empty($service_price)){
                $temp['service_price'] = $service_price;
            }
            if (!empty($service_unit)){
                $temp['service_unit'] = $service_unit;
            }
            if(!empty($bp_cover)){
                $temp['service_cover'] = $bp_cover;
            }
            if (!empty($comment_info)){
                $temp['service_description'] = $comment_info;
                $temp['service_intro'] = $comment_info;
            }
            $temp['service_is_sell'] = 1;
            $temp['service_state'] = 1;
            $temp['service_addtime'] = date("Y-m-d H:i:s", time());
            
            if ($data->update($temp)){
                $this->id = $data->service_id;
                $this->success = 1;
            }
            else {
                $this->id = $data->service_id;
            }
            
            
        } else {

            //添加服务
            $q = new BeautyParlorService();
            $q->beauty_parlor_id = $bp_id;
            $q->service_cover = $bp_cover;
            $q->service_name = $service_name;
            $q->service_description = $comment_info;
            $q->service_intro = $comment_info;
            $q->service_price = empty($service_price)?0:$service_price;
            $q->service_unit = empty($service_unit)?0:$service_unit;
            $q->service_state = 1;
            $q->service_is_sell = 1;
            $q->service_addtime = date("Y-m-d H:i:s", time());
            
            if ($q->save()){
                $this->id = $q->service_id;
                $this->success = 1;
            }
            else{
                $this->id = $q ->service_id;
                $this->success = 0;
            }
        }

        $this->setResult([
            'id' => $this->id,
            'success' => $this->success
        ]);
    }

}